Ext.namespace('VAS');

VAS.SubmitButton = function(config) {
	VAS.SubmitButton.superclass.constructor.call(this, config);
}

Ext.extend(VAS.SubmitButton,Ext.Button, {
	
	text       : "提交",

	url        : "",

	eventId    : "",

	actionType : "form",

	method     : "POST",

	params     : {},
	
	
    initComponent : function(){
        VAS.SubmitButton.superclass.initComponent.call(this);
        this.addEvents(

            'beforeSubmit',

            'afterSubmit',
			
			'submitError'
        );
    },

	handler : function() {
		_this = this;
		if (this.actionType == "ajax") {
			if (this.fireEvent('beforeSubmit',this)) {
	  		    Ext.Ajax.request({
					   url    : this.url,
					   method : this.method,
					   params : this.params,					   
					   success: function(response) {
					   		//检查session是否过期
							checkTimeOut(response);
						   var responseObj = Ext.util.JSON.decode(response.responseText);
						   if(responseObj.success) {
								this.fireEvent('afterSubmit',this,responseObj);
						   } else {
						   		if(responseObj.msg == '' ||  responseObj.msg == undefined){
						   			responseObj.msg = '失败';
						   		}
								Ext.Msg.alert('提示',responseObj.msg,function(bt){
									this.fireEvent('submitError',this,responseObj);
								});
						   }
					   }, failure: function(response) {
							Ext.Msg.alert('提示','服务器异常！');
					   }
			   });			
			}
		}  else {
			var eventForm  = Ext.getCmp(this.eventId).getForm();		
			if (this.fireEvent('beforeSubmit',eventForm,this)) {
				if (eventForm.isValid()){
						eventForm.submit({
							  url              : this.url,
							  method           : this.method,
							  waitTitle        : '正在提交',
							  waitMsg          : '正在向服务器发送请求',
							  clientValidation : true,
							  params           : this.params,
							  success : function (form,action){
									_this.fireEvent('afterSubmit',form,action);
							  },
							  failure : function (form,action){
									if(action.failureType == Ext.form.Action.CLIENT_INVALID){
										Ext.Msg.alert('失败','输入的信息不符合规格，请按照提示重新输入！');
									}else{
										Ext.Msg.alert("失败",action.result.msg);
									}
									_this.fireEvent('submitError',form,action);
							 }		
						});
				}
			}		
		}
	}
 

});
Ext.reg('submitbutton',VAS.SubmitButton);

